package com.ruoyi.common.utils;

import org.apache.commons.codec.binary.Base64;
import org.springframework.beans.factory.annotation.Autowired;

import javax.crypto.Cipher;
import java.security.*;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;

/**
 * RSA加密解密
 * 
 * @author ruoyi
 **/
public class RsaUtils
{

    private final static String serverPriKey = "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCvB5J7/soQ31JPoyPaKx8pAXyTW++wHslINK5byGiQulmDih01b4+sE44/o5kvM4UfwKejU1te4eARKWUieyWoG/b9xQqaifdjpBGeVxWYWhQD4uJ94kiZ6uKikWty7o5uN5G7Qh2uc2tQDqBTAKEzW3a9MtqJ6B2DuKWi8OfC96oaaYOba+gBlvGxSAZDRN/9DBgKgOC2QmqGSxiAvklR8MyahDseP6cQ7KqeoWC1wXA9phPKoWS37UzPGTPRJ95MsPsvS1GoWB37hv6u979G8dCy9bUYQ3Kxv+a//ZWAyiRPgRKEIp74e6X3B3FDR/xTLJl8gmuAgJ4BRAHciB6fAgMBAAECggEBAI9YKz7Dn2eMDhRMrqsZj858ejMRI07hF8W752Qh0IlfJLpW3a8Kq+aoqWIzMNx4XDlyBZj8RqOBZ4mGll5T7rYsGO8idZm1DlebYhaEiZoZg7TfFtE0BnOP64ZqhE4/U4Sum/p5nmdl/Wg2YlU3+c1exfMTd+ljoPBkw7qbg6Om5kGACK42VY8js5C+L9SS+VvTkOKlOXBZa8cQutzimqTE0L6DTNTiu4hCib3wG0QsCSPFLk+3qXNh+jwsVS6o0yxN9NIo8dV/5Hn96kf0p6otYz7phqUdFudEetyHBrTUR8DcuT4FF6eCh4bZYRIFDxQHCJK6mQgdgZmpUSMjkdECgYEA8ASidBwQbea68vAp+q5JnTffwdFuMLKluq+ZHDPksQrsX/XASD00UtpOLIItqCqKY3G7Q+EqFcBno836hqqmMD2uiQMo3bGeQ/r7P6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HZmDrKINRyx1Fwk5NUwBlhUL28+N3Ob6NS9BrZJqaJIie/FOHCZIv2UHyF55MovrjKAskSXGlV3PczJjPSIZynAxE6UTcsfnOXqEgg9j0azjl8JhqHcemeW19+vu5qAL5dAgb8X6QKBgDzzLIcs3Qcv0xXC4PeAqEGBlpitIv6xe4TARgg0S1LYOm4KHsjjkXfScc/bRvdpXJ+YQ088YPbKzNxd0+GOazpPBoQyRW7O0aIo+Yj24nmP6XRcEqfzudUmaO6tyWW2gcgPVi1iZ5KrLOcEjQHt4zsdFEJxvCqzsK9XBit7GenFAoGBAIsyl+O2pBXtnaXweJxdy2GOD0Pjq6HhCovKrCGQ01+ZoiDFYKoutg32oHfv67/Tp+G3tJ1BdJLc3FiHqLXdZwHi2croenr8FD5vRS4wFwQT1GEv6JJTfHKK/nbnr/f/RTzm/urMY5lEYgg1PHN3wP35UM0aqieHnmlvu1/m0w95AoGBAL5Ih0MMwVd21xqfZY/xgaWsYmOAACGN5ObRDWx6Lbp96BvcpZEsxd2Iz4ANq3yt+XkYVrxWXtebPB/CpMboOPbOke31610TyF6fNnrXDNmnSOmGfmTsBic+8hM7YypnFJq9+3jdJmQArHbzKSHuMUolzMWWLWMVuHVfZhj69dS6";


    /**
     * 私钥解密
     * @param privateKeyText
     * @param text
     * @return
     * @throws Exception
     */
    public static String decryptByPrivateKey(String privateKeyText, String text) throws Exception {
        PKCS8EncodedKeySpec pkcs8EncodedKeySpec5 = new PKCS8EncodedKeySpec(Base64.decodeBase64(privateKeyText));
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        PrivateKey privateKey = keyFactory.generatePrivate(pkcs8EncodedKeySpec5);
        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.DECRYPT_MODE, privateKey);
        byte[] result = cipher.doFinal(Base64.decodeBase64(text));
        return new String(result);
    }

    /**
     * 公钥解密
     *
     * @param publicKeyText
     * @param text
     * @return
     * @throws Exception
     */
    public static String decryptByPublicKey(String publicKeyText, String text) throws Exception {
        X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(Base64.decodeBase64(publicKeyText));
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        PublicKey publicKey = keyFactory.generatePublic(x509EncodedKeySpec);
        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.DECRYPT_MODE, publicKey);
        byte[] result = cipher.doFinal(Base64.decodeBase64(text));
        return new String(result);
    }


    /**
     * 公钥加密
     *
     * @param publicKeyText
     * @param text
     * @return
     */
    public static String encryptByPublicKey(String publicKeyText, String text) throws Exception {
        X509EncodedKeySpec x509EncodedKeySpec2 = new X509EncodedKeySpec(Base64.decodeBase64(publicKeyText));
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        PublicKey publicKey = keyFactory.generatePublic(x509EncodedKeySpec2);
        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.ENCRYPT_MODE, publicKey);
        byte[] result = cipher.doFinal(text.getBytes());
        return Base64.encodeBase64String(result);
    }

    /**
     * 私钥加密
     *
     * @param privateKeyText
     * @param text
     * @return
     * @throws Exception
     */
    public static String encryptByPrivateKey(String privateKeyText, String text) throws Exception {
        PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(Base64.decodeBase64(privateKeyText));
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        PrivateKey privateKey = keyFactory.generatePrivate(pkcs8EncodedKeySpec);
        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.ENCRYPT_MODE, privateKey);
        byte[] result = cipher.doFinal(text.getBytes());
        return Base64.encodeBase64String(result);
    }
}